// fetch获取用户列表 渲染到页面 方法
function render_user_content(page = 1, limit = 10) {
  var requestOptions = {
    method: "GET",
    redirect: "follow",
  };

  fetch(
    `${apirUrl}/api/getusers_page?page=${page}&lines=${limit}`,
    requestOptions
  )
    .then((response) => response.json())
    .then((res) => {
      console.log(res);
      //渲染页面
      var user_row_tbody = document.querySelector("#user_row_tbody");
      user_row_tbody.innerHTML = "";
      for (let i = 0; i < res.data.length; i++) {
        var html = `
        <tr id="user_row">
            <th scope="row">${res.data[i].id}</th>
            <td>${res.data[i].username}</td>
            <td>${res.data[i].password}</td>
            <td>${res.data[i].regTime}</td>
            <td>${res.data[i].nickname}</td>
            <td>${res.data[i].email}</td>
            <td>${res.data[i].regAddress}</td>
        <td><button id="user_del_btn" class="btn btn-danger">删除</button></td>
    </tr>
          `;
        user_row_tbody.innerHTML += html;
      }
      //获取user_row 当最后一个td 删除按钮点击 获取到该行 第二列newsid
      var user_row = document.querySelectorAll("#user_row");
      //   console.log(user_row);
      //给删除按钮添加点击事件
      var user_del_btn = document.querySelectorAll("#user_del_btn");
      for (let i = 0; i < user_del_btn.length; i++) {
        user_del_btn[i].onclick = function () {
          console.log(user_row[i].children[0].innerHTML);
          console.log(`点击了删除按钮${user_row[i].children[1].innerHTML}`);
          // 发送请求
          var urlencoded = new URLSearchParams();
          urlencoded.append("id", parseInt(user_row[i].children[0].innerHTML));

          var requestOptions = {
            method: "POST",
            body: urlencoded,
            redirect: "follow",
          };

          fetch(`${apirUrl}/api/delete_user_by_userid`, requestOptions)
            .then((res) => res.json())
            .then((res) => {
              console.log(res);
              if (res.status == 0) {
                console.log("删除成功");
                me_uto.innerHTML = "删除成功";
                toast_body.innerHTML = "成功删除该用户！";
                toast.show();
                //重新获取新闻页数
                get_user_page();
                //重新获取新闻内容
                render_user_content(1);
              } else {
                console.log("删除失败");
              }
            });
        };
      }
    })
    .catch((error) => console.log("error", error));
}
render_user_content();
//fetch  获取用户页数
var user_page_select = document.querySelector("#user_page_select");
function get_user_page(num = 10) {
  fetch(`${apirUrl}/api/getusers_page_num?lines=${num}`)
    .then((res) => res.json())
    .then((res) => {
      console.log(res);
      // 清空select
      user_page_select.innerHTML = "";
      // 渲染
      for (let i = 1; i <= res.pageNum; i++) {
        var html = `
            <option value="${i}">${i}</option>
            `;
        user_page_select.innerHTML += html;
      }
    });
}
get_user_page();

user_page_select.addEventListener("change", function () {
  render_user_content(this.value);
});
